package com.sifa.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sifa.springcloud.entities.CommentResult;
import com.sifa.springcloud.entities.Payment;
import com.sifa.springcloud.myHandler.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimiterController {

    @GetMapping("/resource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommentResult byResource(){
        return new CommentResult(200,"按资源名称限流OK" ,new Payment(2020,"serial001"));
    }
    public CommentResult handleException(BlockException blockException){
         return  new CommentResult(444,blockException.getClass().getCanonicalName()+"\t服务不可用");
    }
    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommentResult byUrl(){

        return new CommentResult(200,"按Url限流测试ok",new Payment(2020,"serial002"));
    }
    @GetMapping("ratelimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException2")
    public CommentResult byCustomer(){
        return new CommentResult(200,"按客户自定义",null);
    }
}
